﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using Lidgren.Network;
using Newtonsoft.Json;

public class GameManager : SingletonMono<GameManager>
{
    public bool isGameSence = false;
    public GameObject tankPrefab;
    public SceneCtrl sceneCtrl;
    //Dictionary<int, TankActor> allActor_Dic = new Dictionary<int, TankActor>();
    List<TankActor> allActor_List = new List<TankActor>();

    public void InitGame(RoomData data)
    {
        tankPrefab = Resources.Load<GameObject>("Prefabs/TankPlayer/TankRenderers");

        StartCoroutine(LoadScene(data));
       
    }
    IEnumerator LoadScene(RoomData data)
    {
        MapData map = data.map;
        SceneManager.LoadScene(map.mapScene);
        //while (!asyncOp.isDone) ;
        yield return SceneManager.LoadSceneAsync(map.mapScene);
        isGameSence = true;
        Log.Info("Load Scene End..");
        UIManager.Instantce.CloseAll();
        sceneCtrl = GameObject.FindObjectOfType<SceneCtrl>();
        

        SpwanActor();
       

        foreach (var msg in delayMsg)
        {
            HandleGameMsgImpl(msg);
        }
        delayMsg.Clear();
        //yield return null;
    }
    private void SpwanActor()
    {
        if (!RoomMgr.Instantce.isMyRoom)
            return;
        foreach (var actor in RoomMgr.Instantce.curRoom.allPlayers)
        {
            var pos = sceneCtrl.GetRandomSpwanPos();
            var player = SpwanActor(actor, pos);

            SendPlayerSpwan(actor, pos);
        }
    }
    private GameObject SpwanActor(ActorData actor,Vector3 pos)
    {
        Log.Info("Spwan Actor " + actor.ToString());
        GameObject player = Instantiate(tankPrefab);
        player.transform.position = pos;

        var tank = player.GetComponent<NetTankActor>();
        tank.InitActorInfo(actor);
        tank.InitStart();
        allActor_List.Add(tank);
        NetworkSyncManager.Instantce.AddSyncActor(tank);
        if (Global.Instantce.Actor.actorGUID == actor.actorGUID)
        {
            CameraManager.Instantce.SetTarget(player);
        }
           
        return player;
    }

    public void SendPlayerSpwan(ActorData actor, Vector3 pos)
    {
        var msg = NetHandler.Instantce.CreateMessage();
        msg.Write(NetConst.OnPlayerSpwan);
        msg.Write(actor.ToString());
        msg.Write(pos);
        NetHandler.Instantce.SendNetMsgToAll(msg);
    }

    public void HandlePlayerSpwan(NetIncomingMessage msg)
    {
        msg.Position = 0;
        var netConst = msg.ReadByte();
        var actor_string = msg.ReadString();
        var pos = msg.ReadVector3();
        var actor = JsonConvert.DeserializeObject<ActorData>(actor_string);
        var player = SpwanActor(actor, pos);
     
    }
    public void HandleGameMsg(NetIncomingMessage msg)
    {
        if(!isGameSence)
        {
            Log.Info("Handle DelayGame Msg");
            delayMsg.Add(msg);
        }
        else
        {
            HandleGameMsgImpl(msg);

        }
    }

    private void HandleGameMsgImpl(NetIncomingMessage msg)
    {
        if (msg == null)
            return;
        msg.Position = 0;
        var netConst = msg.ReadByte();
        switch (netConst)
        {
            case NetConst.OnObjectUpdate:
            case NetConst.OnPlayerUpdate:
            case NetConst.OnPlayerShot:
                NetworkSyncManager.Instantce.OnHandleSyncMsg(msg);
                break;
            case NetConst.OnPlayerSpwan:
                GameManager.Instantce.HandlePlayerSpwan(msg);
                break;
        }
    }
   
    List<NetIncomingMessage> delayMsg = new List<NetIncomingMessage>();
}

